package com.lvjian.community.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.lvjian.community.domain.User;
import com.lvjian.community.domain.UserAssociationRole;
import com.lvjian.community.service.UserAssociationRoleService;
import com.lvjian.community.service.UserRegisterService;
import com.lvjian.community.utils.Consts;

/**
 * 用户注册Controller
 */
@Controller
public class UserRegisterController {

    @Autowired
    private UserRegisterService userRegisterService;
    @Autowired
    private UserAssociationRoleService userAssociationRoleService;

    // 判断是否注册成功
    @RequestMapping(value = "/register/status", method = RequestMethod.POST)
    @ResponseBody
    public Object registerStatus(HttpServletRequest request, HttpSession session) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Integer> relosHashMap = Consts.userRelosHashMap();
        String name = request.getParameter("username");
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        String role = request.getParameter("role");
        String phone = request.getParameter("phone");
        boolean nameFlag = userRegisterService.isOrNotExistUserName(name);
        if (Boolean.TRUE.equals(nameFlag)) {
            boolean emailFlag = userRegisterService.isOrNotExistUserEmail(email);
            if (Boolean.TRUE.equals(emailFlag)) {
                User user = new User();
                user.setUsername(name);
                user.setPassword(password);
                user.setPhone(phone);
                user.setEmail(email);
                User insertUser = userRegisterService.insertUser(user);
                UserAssociationRole userAssociationRole = new UserAssociationRole();
                userAssociationRole.setUserId(insertUser.getId());
                userAssociationRole.setRoleId(relosHashMap.get(role));
                userAssociationRoleService.insertUserAssociationRole(userAssociationRole);
                if (insertUser.getId() != null) {
                    jsonObject.put(Consts.CODE, 1);
                    jsonObject.put(Consts.MSG, "注册成功");

                } else {
                    jsonObject.put(Consts.CODE, 0);
                    jsonObject.put(Consts.MSG, "注册失败");
                }
            } else {
                jsonObject.put(Consts.CODE, 0);
                jsonObject.put(Consts.MSG, "邮箱已被注册");
            }
        } else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "用户名已被注册");
        }
        // try {

        // } catch (Exception e) {
        // jsonObject.put(Consts.CODE, 0);
        // jsonObject.put(Consts.MSG, e.toString());
        // }
        return jsonObject;
    }
}
